/*
 * 包含通用的库接口，包括一些内存容器的封装等
 * */
#include <stdio.h>
#include <unistd.h>
#include "rbtree.h"

int Read(int fd, char *buf, size_t size, int *err)
{
	int len = 0, tmp_len;
	if (buf == NULL || size <= 0) {
		if (err != NULL)
			*err = 0;
		return 0;
	}
	while ((tmp_len = read(fd, buf + len, size - len)) && tmp_len != -1) {
		if (tmp_len == (size - len)) {
			len += tmp_len;
			break;
		}
		len += tmp_len;
	}

	if (tmp_len == -1 && err != NULL)
		*err = -1;
	else if (tmp_len != -1 && err != NULL)
		*err = 0;

	return len;
}

int Write(int fd, const char *buf, size_t size, int *err)
{
	int len = 0, tmp_len;
	if (buf == NULL || size <= 0) {
		if (err != NULL)
			*err = 0;
		return 0;
	}
	while ((tmp_len = write(fd, buf + len, size - len)) && tmp_len != -1) {
		len += tmp_len;
	}
	if (tmp_len == -1 && err != NULL)
		*err = -1;
	else if (tmp_len != -1 && err != NULL)
		*err = 0;

	return len;
}
